/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.mushroom.website.web;

import cz.muni.fi.pa165.mushroom.website.dto.ForestDto;
import cz.muni.fi.pa165.mushroom.website.servicesImpl.ForestServiceImpl;
import java.util.List;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.LocalizableMessage;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;

import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;


/**
 * Action bean for forests
 * 
 * @author Veronika Lovrantova
 */

public class ForestActionBean extends BasicActionBean{
    
    private ActionBeanContext context;
 
    @SpringBean
    protected ForestServiceImpl forestServiceImpl;
    
    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "name", required = true),
        @Validate(on = {"add", "save"}, field = "description", required = false)
    })
    private ForestDto forestDto;
    
    @DefaultHandler
    public Resolution all() {
        return new ForwardResolution("/listForests.jsp");

    }
    
    public Resolution add() {
        forestServiceImpl.createForest(forestDto);
        getContext().getMessages().add(new LocalizableMessage("forest.add.message", escapeHTML(forestDto.getName())));
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution delete() {             
          forestServiceImpl.removeForest(forestDto); 
          getContext().getMessages().add(new LocalizableMessage("forest.delete.message", escapeHTML(forestDto.getName())));
          return new RedirectResolution(this.getClass(), "all");        
    }   
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadForestFromDatabase() {
        String ids = context.getRequest().getParameter("forestDto.id");
        if (ids == null) {
            return;
        }
        forestDto = forestServiceImpl.findForest(Long.parseLong(ids));
    }

     public Resolution edit() {
        return new ForwardResolution("/protected/editForest.jsp");
    }
 
    public Resolution save() {
        forestServiceImpl.updateForest(forestDto);
        getContext().getMessages().add(new LocalizableMessage("forest.update.message", escapeHTML(forestDto.getName())));
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution cancel(){
        return new RedirectResolution(this.getClass(), "all"); 
    }
    
    
    public ForestDto getForestDto() {
        return forestDto;
    }
    
    public void setForestDto(ForestDto forestDto) {
        this.forestDto = forestDto;
    }
    
    public List<ForestDto> getForest() {
        return forestServiceImpl.listForests();
    }
    
    
    @Override
    public void setContext(ActionBeanContext abc) {
        this.context = abc;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }
}